<?php

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\Section as SectionModel;
class Section extends Base {

    public function index(){
        if (is_post()){
            $list = db('section')->order('sort','asc')->select();
            return json(['code' => 0,'data' => $list]);
        }
        return $this->fetch();
    }

    public function add(){
        if (request()->isPost()){
            $data = input('post.');
            $model = new SectionModel();
            $result = $model->saveData($data,'add');
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
        $node_list = db('section')
            ->where('status',1)
            ->order('sort','asc')
            ->select();
        $node_list = $this->getTree($node_list);
        $this->assign('node_list',$node_list);
        return $this->fetch();
    }

    public function edit($id){
        if (request()->isPost()){
            $data = input('post.');
            $model = new SectionModel();
            $result = $model->saveData($data,'edit',$id);
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
        $info = db('section')
            ->where('id',$id)
            ->find();
        $node_list = db('section')
            ->where('status',1)
            ->order('sort','asc')
            ->select();

        $node_list = $this->getTree($node_list);
        $this->assign('node_list',$node_list);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function del($id){
        if (request()->isPost()){
            $model = new SectionModel();
            $result =  $model->delData($id);
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
    }

    public function changeStatus($type,$id){
        if (is_post()){
            if ($this->changeRecordStatus('section',$id,1,$type)){
                if ($type == 1){
                    $msg = '栏目已显示';
                    $log_msg = '显示栏目：';
                }else{
                    $msg = '栏目已隐藏';
                    $log_msg = '隐藏栏目：';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function changeRecommendedStatus($type,$id){
        if (is_post()){
            if ($this->changeRecordStatus('section',$id,2,$type)){
                if ($type == 1){
                    $msg = '已推荐';
                    $log_msg = '推荐栏目：';
                }else{
                    $msg = '已取消推荐';
                    $log_msg = '取消推荐栏目：';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function upload(){
        $file = request()->file('file');

        $info = $this->uploadsImg($file, 'uploads/section/');

        if ($info !== false) {
            return json(['code' => 'suc', 'msg' => '图片上传成功', 'url' => $info['path']]);
        } else {
            return json(['code' => 'err', 'msg' => '图片上传失败，请重试']);
        }
    }
}